package com.qf.util;

import com.qf.entity.Admin;
import com.qf.entity.Student;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * static 修饰的成员是类级别的成员   全局只有一份
 * 非static   修饰的成员是对象级别的成员  每个类都有一份
 *  数据库的存储工具--暂时充当数据库的作用
 */
public class DateUtils {

    private static List<Admin> admins = new ArrayList<>();
    private static List<Student> students = new ArrayList<>();

    /**
     * 添加管理员
     */
    static {
        Admin admin = new Admin();
        admin.setUsername("admin");
        admin.setPassword("123456");
        admin.setName("坤坤");
        admin.setSex("男");
        admin.setJiguan("广东省");
        admin.setPower(Arrays.asList("1", "2", "3"));

        addAdmin(admin);

        Student student = new Student();
        student.setName("小明");
        student.setAge(18);
        student.setSex(1);
        addStudent(student);

        Student student2 = new Student();
        student2.setName("小红");
        student2.setAge(18);
        student2.setSex(0);
        addStudent(student2);


        Student student3 = new Student();
        student3.setName("小刚");
        student3.setAge(20);
        student3.setSex(1);
        addStudent(student3);


    }

    public static void addAdmin(Admin admin) {
        admins.add(admin);
    }

    public static Admin queryAdmin(String username, String password) {
        for (Admin admin : admins) {
            if (admin.getUsername().equals(username) && admin.getPassword().equals(password)) {
                return admin;
            }
        }
        return null;
    }

    public static void addStudent(Student student) {
        students.add(student);

    }

    public static List<Student> queryStudent() {

        return new ArrayList<>(students);
    }

    public static boolean deleteStudent(String name) {
        for (Student student : new ArrayList<>(students)) {
            if (student.getName().equals(name)) {
                students.remove(student);
                return true;
            }
        }
        return false;
    }

}